package aip.core;

import java.beans.PropertyEditorSupport;
import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.commons.lang.StringUtils;

public class OrderedMapEditor extends PropertyEditorSupport
{
    @Override
    public void setAsText(String text) throws IllegalArgumentException
    {
        ListOrderedMap map = new ListOrderedMap();
        for (String line : text.split(","))
        {
            line = StringUtils.trimToNull(line);
            if (line != null)
            {
                String[] d = line.split("=", 2);
                String key = null, value = null;
                if (d.length > 1)
                {
                    value = StringUtils.trimToNull(d[1]);
                }
                if (d.length > 0)
                {
                    key = StringUtils.trimToNull(d[0]);
                    if (key != null)
                    {
                        map.put(key, value);
                    }
                }
            }
        }
        setValue(map);
    }
}
